﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Fcdbas.Core.Util
{
    public class NumberUtil
    {
        /// <summary>
        /// 中国式四舍五入,一般C#用的是银行家算法
        /// </summary>
        /// <param name="value"></param>
        /// <param name="decimals"></param>
        /// <returns></returns>
        public static double RoundChina(double value, int decimals)
        {
            if (value < 0)
            {
                return Math.Round(value + 5 / Math.Pow(10, decimals + 1), decimals, MidpointRounding.AwayFromZero);
            }
            else
            {
                return Math.Round(value, decimals, MidpointRounding.AwayFromZero);
            }
        }

        /// <summary>
        /// 中国式四舍五入,一般C#用的是银行家算法
        /// </summary>
        /// <param name="value"></param>
        /// <param name="decimals"></param>
        /// <returns></returns>
        public static decimal RoundChina(decimal value, int decimals)
        {
            if (value < 0)
            {
                return Math.Round(value + (decimal)5 / (decimal)Math.Pow(10, decimals + 1), decimals, MidpointRounding.AwayFromZero);
            }
            else
            {
                return Math.Round(value, decimals, MidpointRounding.AwayFromZero);
            }
        } 
    }
}
